<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------

namespace app\common\model\content;

use app\common\model\Base;

use think\db;

class Comment extends Base
{
    protected $name = 'content_comment';


    public function content()
    {
        return $this->hasOne('app\common\model\content\Content', 'id', 'content_id');
    }

    public function member()
    {
        return $this->hasOne('app\common\model\member\member', 'id', 'content_id');
    }

    /**
     * User:Simony
     * Function: 列表
     * @param $condition
     * @param int $page
     * @param string $field
     * @param string $order
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function getContentCommentList($condition = [], $page = 0, $field = 'c_c.*,m.nickname', $order = 'c_c.id desc')
    {

        $res = $this->alias('c_c')
            ->join('member m', 'c_c.member_id=m.id', 'left')
            ->join('content c', 'c_c.content_id = c.id')
            ->where($condition)
            ->field($field)
            ->order($order);
//echo $res->buildSql();die;

        if ($page) {
            $res = $res->paginate($page, false, ['query' => request()->param()]);
            $this->page_info = $res;
            $res = $res->toArray();
            return $res['data'];
        } else {
            return $res->select()->toArray();
        }
    }


    //-------------------------------------------------获取树-----------------------------------------------///
    public function getTree($parent_id, $where, $field, $order = 'id desc')
    {
        $data = $this->getContentCommentList($where, 0, $field, $order);
        return $this->_reSort($data, $parent_id);
    }
    private function _reSort($data, $parent_id = 0, $level = 0, $isClear = TRUE)
    {
        static $ret = array();
        if ($isClear) $ret = array();
        foreach ($data as $k => $v) {
            if ($v['pid'] == $parent_id) {
                $v['level'] = $level;
                $ret[] = $v;
                unset($data[$k]); //剔除递归消耗
                $this->_reSort($data, $v['id'], $level + 1, FALSE);
            }
        }
        return $ret;
    }


    /**
     * User:Simony
     * Function:
     * @param $id
     * @return array
     */
    public function getChildren($id, $where = [], $field = '*')
    {
        $data = $this->field($field)->where($where)->select();

        return $this->_children($data, $id);
    }

    private function _children($data, $parent_id = 0, $isClear = TRUE)
    {
        static $ret = array();
        if ($isClear)
            $ret = array();
        foreach ($data as $k => $v) {
            if ($v['pid'] == $parent_id) {
                $ret[] = $v['id'];
                $this->_children($data, $v['id'], FALSE);
            }
        }
        return $ret;
    }

}

?>
